﻿Public Class FDITEM
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Dim connect As CONDB = CONDB.NewConnection()
    Dim dtsetFditem As DataSet
    Dim s1, s2, s3 As FILTERCLASS

    Private Sub filterCCode()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(ccode AS CHAR(6)) AS ccode, name FROM finchcode WHERE status = '1'"
        s = New FILTERCLASS(txtCcode, sql, "รหัสCcode,Ccode", "80,200", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterRCode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s1.SetSql = sql
    End Sub

    Private Sub filterRCodeByCcode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1' AND ccode = '" & txtCcode.Tag & "'"
        s1.SetSql = sql
    End Sub

    Private Sub filterICCode()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(ccode AS CHAR(6)) AS ccode, name FROM finchcode WHERE status = '1'"
        s = New FILTERCLASS(txtIccode, sql, "รหัสCcode,Ccode", "80,200", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterIRCode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s2.SetSql = sql
    End Sub

    Private Sub filterIRCodeByICcode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1' AND ccode = '" & txtIccode.Tag & "'"
        s2.SetSql = sql
    End Sub

    Private Sub filterHCCode()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(ccode AS CHAR(6)) AS ccode, name FROM finchcode WHERE status = '1'"
        s = New FILTERCLASS(txtHccode, sql, "รหัสCcode,Ccode", "80,200", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterHRCode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s3.SetSql = sql
    End Sub

    Private Sub filterHRCodeByHCcode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1' AND ccode = '" & txtHccode.Tag & "'"
        s3.SetSql = sql
    End Sub

    Private Sub filterBUnit()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(unitid AS CHAR(6)) AS unitid, unitname_th, unitname_en FROM masunit WHERE status = '1'"
        s = New FILTERCLASS(txtB_unit, sql, "IDหน่วย,หน่วยไทย,หน่วยอังกฤษ", "80,200,200", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterSUnit()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(unitid AS CHAR(6)) AS unitid, unitname_th, unitname_en FROM masunit WHERE status = '1'"
        s = New FILTERCLASS(txtS_unit, sql, "IDหน่วย,หน่วยไทย,หน่วยอังกฤษ", "80,200,200", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub queryFditem()
        dtsetFditem = New DTSETFDITEM

        Dim objGetFditem As New FDITEMCLASS
        Dim sql As String = objGetFditem.selectFditem_
        connect.GetTable(sql, dtsetFditem.Tables("fditemJoinMasproduct"))

        connect.Dispose()

        xgvFDITEM.DataSource = dtsetFditem
        xgvFDITEM.ForceInitialize()
    End Sub

    Private Sub SROOMITEM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        queryFditem()

        filterCCode()
        filterICCode()
        filterHCCode()
        filterBUnit()
        filterSUnit()

        Dim sql1 As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s1 = New FILTERCLASS(txtRcode, sql1, "รหัสRcode,Rcode", "80,200", "1,1", "1,1")
        s1.SetShowBorder = True

        Dim sql2 As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s2 = New FILTERCLASS(txtIrcode, sql2, "รหัสRcode,Rcode", "80,200", "1,1", "1,1")
        s2.SetShowBorder = True

        Dim sql3 As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s3 = New FILTERCLASS(txtHrcode, sql2, "รหัสRcode,Rcode", "80,200", "1,1", "1,1")
        s3.SetShowBorder = True

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub txtCcode_TextChanged(sender As Object, e As EventArgs) Handles txtCcode.TextChanged
        If Convert.ToString(txtCcode.Tag) <> "" Then
            filterRCodeByCcode()
        Else
            filterRCode()
        End If
    End Sub

    Private Sub txtIccode_TextChanged(sender As Object, e As EventArgs) Handles txtIccode.TextChanged
        If Convert.ToString(txtIccode.Tag) <> "" Then
            filterIRCodeByICcode()
        Else
            filterIRCode()
        End If
    End Sub

    Private Sub txtHccode_TextChanged(sender As Object, e As EventArgs) Handles txtHccode.TextChanged
        If Convert.ToString(txtHccode.Tag) <> "" Then
            filterHRCodeByHCcode()
        Else
            filterHRCode()
        End If
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        txtFodid.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("fodid").ToString()
        txtFood.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("food").ToString()

        txtPrdcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("prdcode").ToString()

        txtCcode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("ccode").ToString()
        txtCcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("cname").ToString()

        txtRcode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("rcode").ToString()
        txtRcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("rname").ToString()

        txtIccode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("iccode").ToString()
        txtIccode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("icname").ToString()

        txtIrcode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("ircode").ToString()
        txtIrcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("irname").ToString()

        txtHccode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("hccode").ToString()
        txtHccode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("hcname").ToString()

        txtHrcode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("hrcode").ToString()
        txtHrcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("hrname").ToString()

        txtB_unit.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("b_unit").ToString()
        txtB_unit.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("buyUnit").ToString()

        txtS_unit.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("s_unit").ToString()
        txtS_unit.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("sellUnit").ToString()

        txtOpdprc.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("opdprc").ToString()
        txtIpdprc.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("ipdprc").ToString()

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("status") IsNot DBNull.Value Then
            ckbFditemStatus.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("status"))
        Else
            ckbFditemStatus.Checked = False
        End If

        linePrdcode.Visible = True
        lblPrdcode.Visible = True
        txtPrdcode.Visible = True
    End Sub

    Private Sub btnAdddata_Click(sender As Object, e As EventArgs) Handles btnAdddata.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If txtFood.Text.Length.ToString > 0 Then
                    Dim objAddmasproduct As New MASPRODUCT
                    'objAddmasproduct.CODE_ = txtBedscode.Text.Trim
                    objAddmasproduct.PRDNAME_ = txtFood.Text.Trim

                    If txtCcode.Tag <> "" Then
                        objAddmasproduct.CCODE_ = txtCcode.Tag
                    End If

                    If txtRcode.Tag <> "" Then
                        objAddmasproduct.RCODE_ = txtRcode.Tag
                    End If

                    If txtIccode.Tag <> "" Then
                        objAddmasproduct.ICCODE_ = txtIccode.Tag
                    End If

                    If txtIrcode.Tag <> "" Then
                        objAddmasproduct.IRCODE_ = txtIrcode.Tag
                    End If

                    If txtHccode.Tag <> "" Then
                        objAddmasproduct.HCCODE_ = txtHccode.Tag
                    End If

                    If txtHrcode.Tag <> "" Then
                        objAddmasproduct.HRCODE_ = txtHrcode.Tag
                    End If

                    If txtB_unit.Tag <> "" Then
                        objAddmasproduct.B_UNIT_ = txtB_unit.Tag
                    End If

                    If txtS_unit.Tag <> "" Then
                        objAddmasproduct.S_UNIT_ = txtS_unit.Tag
                    End If

                    If txtOpdprc.Text <> "" Then
                        objAddmasproduct.OPDPRC_ = txtOpdprc.Text.Trim
                    End If

                    If txtIpdprc.Text <> "" Then
                        objAddmasproduct.IPDPRC_ = txtIpdprc.Text.Trim
                    End If

                    objAddmasproduct.PRDCAT_ = 7
                    'objAddmasproduct.MPDSTATUS_ = ckbFditemStatus.Checked

                    Dim sqlMasproduct As String = objAddmasproduct.makeMasproductInsertSQL

                    Dim objAddFDITEM As New FDITEMCLASS
                    objAddFDITEM.food_ = txtFood.Text.Trim
                    objAddFDITEM.foodStatus_ = ckbFditemStatus.Checked
                    objAddFDITEM.masproductSQL_ = sqlMasproduct

                    If objAddFDITEM.insertFDITEM = True Then
                        queryFditem()
                        clearForm()

                        linePrdcode.Visible = False
                        lblPrdcode.Visible = False
                        txtPrdcode.Visible = False
                    End If
                Else
                    MsgBox("ท่านต้องระบุข้อมูลชื่ออาหาร")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub btnEditData_Click(sender As Object, e As EventArgs) Handles btnEditData.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลอาหารใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    If txtFood.Text.Length.ToString > 0 And txtFodid.Text.Length.ToString > 0 Then
                        Dim objEditmasproduct As New MASPRODUCT
                        objEditmasproduct.MPDPRDCODE_ = txtPrdcode.Text

                        If txtCcode.Tag <> "" Then
                            objEditmasproduct.CCODE_ = txtCcode.Tag
                        End If

                        If txtRcode.Tag <> "" Then
                            objEditmasproduct.RCODE_ = txtRcode.Tag
                        End If

                        If txtIccode.Tag <> "" Then
                            objEditmasproduct.ICCODE_ = txtIccode.Tag
                        End If

                        If txtIrcode.Tag <> "" Then
                            objEditmasproduct.IRCODE_ = txtIrcode.Tag
                        End If

                        If txtHccode.Tag <> "" Then
                            objEditmasproduct.HCCODE_ = txtHccode.Tag
                        End If

                        If txtHrcode.Tag <> "" Then
                            objEditmasproduct.HRCODE_ = txtHrcode.Tag
                        End If

                        If txtB_unit.Tag <> "" Then
                            objEditmasproduct.B_UNIT_ = txtB_unit.Tag
                        End If

                        If txtS_unit.Tag <> "" Then
                            objEditmasproduct.S_UNIT_ = txtS_unit.Tag
                        End If

                        If txtOpdprc.Text <> "" Then
                            objEditmasproduct.OPDPRC_ = txtOpdprc.Text.Trim
                        End If

                        If txtIpdprc.Text <> "" Then
                            objEditmasproduct.IPDPRC_ = txtIpdprc.Text.Trim
                        End If


                        objEditmasproduct.PRDCAT_ = 3

                        'objEditmasproduct.CODE_ = txtBedscode.Text.Trim
                        objEditmasproduct.PRDNAME_ = txtFood.Text.Trim

                        Dim sqlMasproduct As String = objEditmasproduct.makeMasproductEditSQL

                        Dim objEditFditem As New FDITEMCLASS
                        objEditFditem.fodid_ = txtFodid.Text.Trim
                        objEditFditem.food_ = txtFood.Text.Trim
                        objEditFditem.foodStatus_ = ckbFditemStatus.Checked
                        objEditFditem.masproductSQL_ = sqlMasproduct

                        If objEditFditem.editFDITEM = True Then
                            queryFditem()
                            clearForm()

                            linePrdcode.Visible = False
                            lblPrdcode.Visible = False
                            txtPrdcode.Visible = False
                        End If
                    Else
                        MsgBox("ท่านต้องระบุข้อมูลชื่ออาหาร")
                    End If
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub clearForm()
        'txtClinic.Tag = ""
        'txtClinic.Clear()
        'txtBedscode.Clear()
        'txtBedsname.Clear()
        'txtRemark.Clear()
        'txtPrdcode.Clear()
        'txtIccode.Tag = ""
        'txtIccode.Clear()
        'txtIrcode.Tag = ""
        'txtIrcode.Clear()
        'txtB_unit.Tag = ""
        'txtB_unit.Clear()
        'txtS_unit.Tag = ""
        'txtS_unit.Clear()
        'txtIpdprc.Text = ""
        'ckbSroomStatus.Checked = False
        'txtBedsid.Clear()
    End Sub

    
End Class